package com.llc.mall.auth.application.command.impl;

import com.llc.mall.auth.application.command.UserAppService;
import com.llc.mall.auth.application.commands.UserLoginCommand;
import com.llc.mall.auth.application.dto.UpdatePasswordDTO;
import com.llc.mall.auth.application.manager.TokenStore;
import com.llc.mall.auth.application.service.AuthAccountService;
import com.llc.mall.auth.domain.gateway.PermissionGateway;
import com.llc.mall.auth.domain.user.entity.User;
import com.llc.mall.auth.domain.user.service.UserDomainService;
import com.llc.mall.auth.infrastructure.persist.model.AuthAccount;
import com.llc.mall.auth.interfaces.vo.TokenInfoVo;
import com.llc.mall.common.core.util.BeanUtil;
import com.mall4j.cloud.api.auth.bo.UserInfoInTokenBO;
import com.mall4j.cloud.api.auth.vo.TokenInfoVO;
import com.mall4j.cloud.api.rbac.dto.ClearUserPermissionsCacheDTO;
import com.llc.mall.common.security.AuthUserContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserAppServiceImpl implements UserAppService {

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private AuthAccountService authAccountService;

    @Resource
    private PermissionGateway permissionGateway;

    @Resource
    private UserDomainService userDomainService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public TokenInfoVo login(UserLoginCommand userLoginCommand) {
        //认证校验
        User user = userDomainService.authenticate(userLoginCommand.getUserName(), userLoginCommand.getPassword());
        //token生成
        // 将以前的权限清理了,以免权限有缓存
        permissionGateway.clearUserPermissionsCache(generateDto(user));
        // 保存token，返回token数据给前端，这里是最重要的
        UserInfoInTokenBO userInfoInTokenBO = new UserInfoInTokenBO();
        userInfoInTokenBO.setUserId(user.getId());
        userInfoInTokenBO.setSysType(user.getSysType());
        userInfoInTokenBO.setUid(user.getUid());
        TokenInfoVO vo =  tokenStore.storeAndGetVo(userInfoInTokenBO);
        return null;
    }

    @Override
    public void loginOut() {
        UserInfoInTokenBO userInfoInToken = AuthUserContext.get();
        ClearUserPermissionsCacheDTO clearUserPermissionsCacheDTO = new ClearUserPermissionsCacheDTO();
        clearUserPermissionsCacheDTO.setSysType(userInfoInToken.getSysType());
        clearUserPermissionsCacheDTO.setUserId(userInfoInToken.getUserId());
        // 将以前的权限清理了,以免权限有缓存
        permissionGateway.clearUserPermissionsCache(clearUserPermissionsCacheDTO);
        // 删除该用户在该系统的token
        tokenStore.deleteAllToken(userInfoInToken.getSysType().toString(), userInfoInToken.getUid());
    }

    @Override
    public void modifyPassword(UpdatePasswordDTO updatePasswordDTO) {
        UserInfoInTokenBO userInfoInToken = AuthUserContext.get();
        AuthAccount authAccount = authAccountService.getByUserIdAndType(userInfoInToken.getUserId(), userInfoInToken.getSysType());
        if (!passwordEncoder.matches(updatePasswordDTO.getOldPassword(), authAccount.getPassword())) {
//            return ServerResponseEntity.showFailMsg("旧密码不正确");
        }
        authAccountService.updatePassword(userInfoInToken.getUserId(), userInfoInToken.getSysType(), updatePasswordDTO.getNewPassword());
        tokenStore.deleteAllToken(userInfoInToken.getSysType().toString(), userInfoInToken.getUid());
    }

    private ClearUserPermissionsCacheDTO generateDto(User user){
        ClearUserPermissionsCacheDTO clearUserPermissionsCacheDTO = new ClearUserPermissionsCacheDTO();
        clearUserPermissionsCacheDTO.setSysType(user.getSysType());
        clearUserPermissionsCacheDTO.setUserId(user.getId());
        return clearUserPermissionsCacheDTO;
    }
}
